from rest_framework import viewsets, mixins
from rest_framework.response import Response

from goods.serializers import FoodCategorySerializer, FoodSerializer
from goods.models import FoodCategory, Foods
from goods.filters import FoodCategoryFilter, FoodFilter


class FoodCategoryView(viewsets.GenericViewSet, mixins.ListModelMixin):
    queryset = FoodCategory.objects.all()
    serializer_class = FoodCategorySerializer
    filter_class = FoodCategoryFilter

    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)


class FoodView(viewsets.GenericViewSet, mixins.ListModelMixin):
    queryset = Foods.objects.all()
    serializer_class = FoodSerializer
    filter_class = FoodFilter

    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        serializer = self.get_serializer(queryset.first())
        return Response(serializer.data)
